<?php

namespace App\Http\Controllers;

use App\Http\Model\Api\FinanceRechargeOrder;
use App\Http\Model\TopupRebateRecharge;
use App\Http\Services\ConfigServices;
use App\Http\Services\ShareDiamondServices;
use App\Http\Services\ShareSendGoodsServices;
use App\Http\Services\UserServices;
use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

/*
 * 充值赠送钻石
 * */

class TopupRebateController extends BaseController
{
    protected $configServices;
    protected $userServices;
    protected $shareDiamondServices;
    protected $prop_17 = 17;//足迹清除卡
    protected $prop_19 = 19;//甜蜜蛋糕
    protected $send_prop_num= 1;//赠送个数
    // protected $award_diamond = 5;//返利奖励比例 5%


    public function __construct()
    {
        //监听充值
        $this->configServices = new ConfigServices();
        $this->userServices = new UserServices();
        $this->shareDiamondServices = new ShareDiamondServices();
    }

    private function award_diamond() {
        $array = [1,2,3,4,5,6,7,8,9,10];
        $index = array_rand($array);
        return $array[$index];
    }
    /*
     *  {"order_id":11973,"order_sn":"1909060426062019090611915","user_id":383,"rule_id":23,"diamond_amount":100000
     * ,"price":100000,"pay_type":5,"status":1,"create_timestamp":1567758366
     * ,"create_time":"2019-09-06 08:26:06","pay_timestamp":1567758366,
     * "out_trade_sn":"1909060426062019090611915","update_time":"2019-09-06 08:26:06",
     * "channel_id":"1000000","pearl_amount":0,"recharge_type":0,"consumer_type":2}
     * */

    public function assignAction($body)
    {
        $body_date = $body['pay_timestamp']; //充值
        $consumer_date = $body_date ?? time();
        $config = $this->configServices->getConfig(14);
        $checkDate = $this->configServices->checkConfigDate($config['topup_rebate_date'], $consumer_date);
        if (!$checkDate) {
            write_log('topUpRebateConsumer', 'timeError', $body);//活动时间错误
            return false;
        }
        // 首次充值随机返利
        if($this->checkIsFirst($body['user_id'])) {
            $this->listenUserRecharge($body);// 充值
        }
        
        return true;
    }
//
//{"order_id":21669,"order_sn":"1912020953232019120217304","user_id":28961,"rule_id":13,"diamond_amount":600
//,"price":600,"pay_type":12,"status":1,"create_timestamp":1575294803,"create_time":"2019-12-02 13:53:23","pay_tim
//estamp":1575294821,"out_trade_sn":"191202215323582205878900","u
//pdate_time":"2019-12-02 13:53:41","channel_id":"700035","pearl_amount":0,"recharge_type":0}

    public function listenUserRecharge($body)
    {
        // todo log
        write_log('topUpRebateConsumer', 'rechargeCallback', $body);

        $user_id = $body['user_id'] ?? 0;
        $order_id = $body['order_id'] ?? 0;
        $price = $body['price'] ?? 0;
        $diamond_num = $body['diamond_amount'] ?? 0;
        $pay_type = $body['pay_type'];

        if($pay_type == 5){
            write_log('topUpRebateConsumer', 'payTypeError', json_encode($body));//代充不计入
            return false;
        }
        //支付类型 --- 0.支付宝 1.微信 2.苹果内购 3.android公号 4.IOS公众号 5.后台代充 6.支付宝wap端充值 7.微信wap端充值
        if (!$user_id || !$diamond_num || !$price || !$order_id) {
            write_log('topUpRebateConsumer', 'argumentError', json_encode($body));//paytype错
            return false;
        }

        $first_data['award_diamond'] = $diamond_num * $this->award_diamond() / 100;
        $first_data['user_id'] = $user_id;
        //发送接口请求
        $recharge = [
            'user_id' => $user_id,
            'diamond_num' => $first_data['award_diamond'],
            'msg' => "首次充值奖励 {$first_data['award_diamond']}钻",
        ];
        $diamond_api = json_decode(curl_request(env('KK_API_HOST') . '/finance/diamond/recharge', $recharge), true);
        if ($diamond_api['code'] != 0) {
            //充值失败 销毁队列
            write_log('topUpRebateConsumer', 'oneUserRechargeError', json_encode($diamond_api));
            return false;
        }
        
        // 发送消息
        $msg_data_one['user_id'] = $user_id;
        $msg_data_one['msg'] = "首次充值奖励 钻石* " . $first_data['award_diamond'];
        curl_request(env('KK_API_HOST') . '/msg/text', $msg_data_one);

        TopupRebateRecharge::createData($user_id, $order_id, $price, $first_data['award_diamond']);

        write_log('topUpRebateConsumer', '首次充值返利成功', $user_id);

        return true;
    }

    /**
     * 记录首次充值返利的用户
     */
    private function checkIsFirst(int $user_id) {
        // $redis_key = "CACHE:TOPUP_REBATE:FIRST";
        // if(Redis::SISMEMBER($redis_key, $user_id)) return false;
        // Redis::SADD($redis_key, $user_id);
        return FinanceRechargeOrder::checkIsRecharge($user_id) <= 1;
        // return true;
    }

}
